package test.com.why.aop;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Random;

public class BST {
    private Node root;
    private int count = 0;

    public void insert(int val) {
        if (find(val, root) != null) {
            System.out.println("不能重复插入");
            return;
        }
        insert(new Node(val));
    }


    private Node insert(Node node) {

        Node temp = root;
        if (root == null) {
            root = node;
            return node;
        }
        count++;
        while (temp != null) {
            if (temp.value > node.value) {
                if (temp.left == null) {
                    temp.left = node;
                    return node;
                }
                temp = temp.left;
            } else {
                if (temp.right == null) {
                    temp.right = node;
                    return node;
                }
                temp = temp.right;

            }
        }
        return node;

    }

    public Node find(int val, Node currentNode) {
        if (currentNode == null)
            return null;
        if (currentNode.value > val) {
            find(val, currentNode.left);
        } else if (currentNode.value < val) {
            find(val, currentNode.right);
        }
        if (currentNode.value == val) {
            return currentNode;
        }
        return null;


    }

    public void deleteNode(int val) {

        if (find(val, root) == null) {
            System.out.println("没有发现该节点");
            return;
        }
        if (delete(val, root, root)) {
            System.out.println("删除成功");
        } else {
            System.out.println("删除失败");
        }

    }

    private boolean delete(int val, Node root, Node pre) {
        if (root == null)
            return false;

        if (root.value == val) {
            if (root.left == null && root.right == null) {
                root = null;
                return true;
            } else if (root.left != null && root.right != null) {

            }
        }

        return true;
    }


    private static class Node {
        int value;
        Node left;
        Node right;

        public Node(int value) {
            this.value = value;
        }
    }

    public void pre() {
        pre(root);
    }

    private void pre(Node node) {
        if (node == null) return;


        pre(node.left);
        System.out.println(node.value);
        pre(node.right);


    }
   private static final Integer offer=0;

    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
        BST bst = new BST();
        Field offer = BST.class.getDeclaredField("offer");
        offer.setAccessible(true);
        Field modifiers = Field.class.getDeclaredField("modifiers");
        modifiers.setAccessible(true);
        modifiers.setInt(offer, offer.getModifiers() & ~Modifier.FINAL);
        offer.set(null, Integer.MAX_VALUE);
        System.out.println(BST.offer);

//        BST bst = new BST();
//
//
//        int j = 0;
//        while (j++ < 100) {
//            for (int i = 0; i < 10; i++) {
//                bst.insert(new Random().nextInt(10));
//            }
//            bst.getCount();
//        }
//        // bst.pre();
//        System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
//        bst.getCount();
//        System.out.println(Integer.MAX_VALUE);
//        System.out.println(Integer.MAX_VALUE % 1024);
    }

}




